package code

import (
	"fmt"
	"math/rand"
	"time"
)

// SMSCodeGen generates a SMS code.
type SMSCodeGen struct {
	nowFunc func() time.Time
}

// NewSMSCodeGen creates a SMSCodeGen.
func NewSMSCodeGen() *SMSCodeGen {
	return &SMSCodeGen{
		nowFunc: time.Now,
	}
}

// GenerateCode generates a [0,num) code.
func (s *SMSCodeGen) GenerateCode(num int) (code string) {
	numLen := len(fmt.Sprintf("%d", num))
	now := s.nowFunc()
	rand.Seed(now.UnixNano())
	num = rand.Intn(num)
	return fmt.Sprintf("%0*d", numLen, num)
}
